#!/usr/bin/make -f
#
# \brief  Tool-chain creation tool for the Genode OS Framework
# \author Norman Feske
# \date   2009-02-03
#

# 13/04/2009 - Amended GCC_DOWNLOAD_URL and BINUTILS_DOWNLOAD_URL to fetch from Google Code

#
# Download locations
#
GCC_DOWNLOAD_URL      = ftp://ftp.fu-berlin.de/unix/languages/gcc/releases
BINUTILS_DOWNLOAD_URL = http://i9os.googlecode.com/files

#
# Tool versions and install location
#
GCC_VERSION      = 4.2.4
BINUTILS_VERSION = 2.18
INSTALL_LOCATION = /usr/local/genode-gcc

#
# Platform-specific configuration
#
ifeq ($(MAKECMDGOALS),x86)
  PLATFORM       = x86
endif

#
# General 'configure' parameters for binutils and gcc
#
PROGRAM_PREFIX  ?= genode-$(PLATFORM)-

BINUTILS_CONFIG += --disable-werror \
                   --prefix=$(INSTALL_LOCATION) \
                   --program-prefix=$(PROGRAM_PREFIX)

GCC_CONFIG      += --prefix=$(INSTALL_LOCATION) \
                   --program-prefix=$(PROGRAM_PREFIX) \
                   --with-gnu-as --with-gnu-ld --disable-tls --disable-threads

#
# Utilities
#
SHELL       = bash
BRIGHT_COL  = \033[01;33m
DEFAULT_COL = \033[0m
ECHO        = @echo -e


help:
	$(ECHO) "Build tool chain for the Genode OS Framework"
	$(ECHO)
	$(ECHO) "The tool chain consists of GCC $(GCC_VERSION) and binutils $(BINUTILS_VERSION)"
	$(ECHO) "and will be installed at $(INSTALL_LOCATION)."
	$(ECHO)
	$(ECHO) "--- available commands ---"
	$(ECHO) "x86       - download, build, and install gcc and binutils for x86"
	$(ECHO) "clean     - clean everything except downloaded archives"
	$(ECHO) "cleanall  - clean everything including downloaded archives"
	$(ECHO) "uninstall - remove installation from $(INSTALL_LOCATION)"

#
# We use the binaries 'objdump' and 'g++' as examples for declaring
# dependencies. All other programs will be generated and built as a side
# effect.
#

build: binutils-$(BINUTILS_VERSION)/binutils/objdump gcc_build_$(PLATFORM)/gcc/g++

install: $(addprefix $(INSTALL_LOCATION)/bin/$(PROGRAM_PREFIX),objdump g++)

x86: build install

binutils-$(BINUTILS_VERSION).tar.gz:
	$(ECHO) "$(BRIGHT_COL)downloading binutils...$(DEFAULT_COL)"
	@wget $(BINUTILS_DOWNLOAD_URL)/binutils-$(BINUTILS_VERSION).tar.gz

gcc-core-$(GCC_VERSION).tar.bz2:
	$(ECHO) "$(BRIGHT_COL)downloading gcc...$(DEFAULT_COL)"
	@wget $(GCC_DOWNLOAD_URL)/gcc-$(GCC_VERSION)/gcc-core-$(GCC_VERSION).tar.bz2

gcc-g++-$(GCC_VERSION).tar.bz2:
	$(ECHO) "$(BRIGHT_COL)downloading g++...$(DEFAULT_COL)"
	@wget $(GCC_DOWNLOAD_URL)/gcc-$(GCC_VERSION)/gcc-g++-$(GCC_VERSION).tar.bz2

gcc-$(GCC_VERSION): gcc-core-$(GCC_VERSION).tar.bz2 gcc-g++-$(GCC_VERSION).tar.bz2
	$(ECHO) "$(BRIGHT_COL)unpacking gcc and g++...$(DEFAULT_COL)"
	@for i in $^ ; do tar xfj $$i ;done

binutils-$(BINUTILS_VERSION)/configure: binutils-$(BINUTILS_VERSION).tar.gz
	$(ECHO) "$(BRIGHT_COL)unpacking binutils...$(DEFAULT_COL)"
	@# we touch the target to override its archived modification time
	@tar xfz $^ && touch $@

binutils-$(BINUTILS_VERSION)/Makefile: binutils-$(BINUTILS_VERSION)/configure
	$(ECHO) "$(BRIGHT_COL)configuring binutils...$(DEFAULT_COL)"
	@cd binutils-$(BINUTILS_VERSION); ./configure $(BINUTILS_CONFIG)

binutils-$(BINUTILS_VERSION)/binutils/objdump: binutils-$(BINUTILS_VERSION)/Makefile
	$(ECHO) "$(BRIGHT_COL)builing binutils...$(DEFAULT_COL)"
	@make -C binutils-$(BINUTILS_VERSION)

$(INSTALL_LOCATION)/bin/$(PROGRAM_PREFIX)objdump: binutils-$(BINUTILS_VERSION)/binutils/objdump
	$(ECHO) "$(BRIGHT_COL)installing binutils... (requires root privileges)$(DEFAULT_COL)"
	@sudo make -C binutils-$(BINUTILS_VERSION) install

gcc_build_$(PLATFORM)/Makefile: gcc-$(GCC_VERSION)
	$(ECHO) "$(BRIGHT_COL)configuring gcc...$(DEFAULT_COL)"
	@mkdir -p gcc_build_$(PLATFORM)
	@cd gcc_build_$(PLATFORM); ../gcc-$(GCC_VERSION)/configure $(GCC_CONFIG)

gcc_build_$(PLATFORM)/gcc/g++: gcc_build_$(PLATFORM)/Makefile
	$(ECHO) "$(BRIGHT_COL)builing gcc...$(DEFAULT_COL)"
	@make -C gcc_build_$(PLATFORM)

$(INSTALL_LOCATION)/bin/$(PROGRAM_PREFIX)g++: gcc_build_$(PLATFORM)/gcc/g++
	$(ECHO) "$(BRIGHT_COL)installing gcc... (requires root privileges)$(DEFAULT_COL)"
	@sudo make -C gcc_build_$(PLATFORM) install

clean:
	rm -rf binutils-$(BINUTILS_VERSION) gcc-$(GCC_VERSION)

cleanall: clean
	rm -rf binutils-$(BINUTILS_VERSION).tar.gz
	rm -rf gcc-core-$(GCC_VERSION).tar.bz2
	rm -rf gcc-g++-$(GCC_VERSION).tar.bz2
	rm -rf gcc_build_$(PLATFORM)

uninstall:
	sudo rm -rf $(INSTALL_LOCATION)
